# -*- encoding: utf-8 -*-
from openerp.osv import fields,osv
from openerp.addons.dm_base import utils

class product_product(osv.osv):
    _inherit = "product.product"
    _columns = {
        'solder_cnt':fields.integer('焊点数', track_visibility='onchange'),
        'solder_method':fields.selection([('ai_ver','立式AI'),('ai_hor','卧式AI'),('smt','SMT'),('none','无')],'焊点工序', track_visibility='onchange', required=True),
        #PCB
        'pcb_file': fields.function(utils.field_get_file, fnct_inv=utils.field_set_file,  type='binary',  string='PCB', multi="_get_file"),
        'pcb_file_name': fields.char('PCB文件名'),
        #原理图
        'schematic_diagram_file': fields.function(utils.field_get_file, fnct_inv=utils.field_set_file,  type='binary',  string='原理图', multi="_get_file"),
        'schematic_diagram_file_name': fields.char('原理图'),
    }
    _defaults={'solder_method':'none'}
    
class mrp_bom(osv.osv):
    _inherit = 'mrp.bom'
    def _solder_cnt_total(self, cr, uid, ids, field_names, args=None, context=None):
        res = {}
        for bom in self.browse(cr, uid, ids, context=context):
            solder_cnt_total = solder_cnt_ai_ver = solder_cnt_ai_hor = solder_cnt_smt = 0
            if not bom.bom_id:
                for bom_line in bom.bom_lines:
                    solder_cnt_total += bom_line.product_id.solder_cnt
                    solder_cnt_ai_ver += bom_line.product_id.solder_method == 'ai_ver' and bom_line.product_id.solder_cnt or 0
                    solder_cnt_ai_hor += bom_line.product_id.solder_method == 'ai_hor' and bom_line.product_id.solder_cnt or 0
                    solder_cnt_smt += bom_line.product_id.solder_method == 'smt' and bom_line.product_id.solder_cnt or 0
            res[bom.id] = {'solder_cnt_total':solder_cnt_total,
                               'solder_cnt_ai_ver':solder_cnt_ai_ver,
                               'solder_cnt_ai_hor':solder_cnt_ai_hor,
                               'solder_cnt_smt':solder_cnt_smt,
                               }
        return res
                    
    _columns = {
        #部件使用
        'solder_method':fields.related('product_id','solder_method',type='selection', 
                                    selection=[('ai_ver','立式AI'),('ai_hor','卧式AI'),('smt','SMT'),('none','无')],
                                    string='焊点工序'),
        'solder_cnt':fields.related('product_id','solder_cnt',type='integer', string='焊点数'),
        #成品使用
        'solder_cnt_total':fields.function(_solder_cnt_total, type='integer', string='总焊点数', multi='_solder_cnt_total'),
        'solder_cnt_ai_ver':fields.function(_solder_cnt_total, type='integer', string='总焊点数', multi='_solder_cnt_total'),
        'solder_cnt_ai_hor':fields.function(_solder_cnt_total, type='integer', string='总焊点数', multi='_solder_cnt_total'),
        'solder_cnt_smt':fields.function(_solder_cnt_total, type='integer', string='总焊点数', multi='_solder_cnt_total'),
    }
